#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: twl
@date: 2020/11/5 11:20
@description: 健康监控
"""
from flask import Blueprint, current_app, jsonify, request
from base.util.json_util import JsonUtil
from clickhouse_driver import Client
from pyhive import hive
from common.feishu import FeishuMsgHandler


health = Blueprint('health', __name__)


@health.route('/healthz', methods=['POST', 'GET'])
def healthz():
    print(current_app.config['BATCH_SAVE_MOBILE_MAC'])
    data = {"status": 1}
    return JsonUtil.response_success(data)

@health.route('/testfeishu', methods=['POST', 'GET'])
def testfeishu():
    FeishuMsgHandler().sendFeiShu("3243")
    return "发送飞书成功"

@health.route('/try_connect_ck', methods=['POST', 'GET'])
def try_connect_ck():
    config_ck = {
        'host': current_app.config['CLICKHOUSE_HOST'],
        'url': current_app.config['CLICKHOUSE_URL'],
        'port': current_app.config['CLICKHOUSE_PORT'],
        'user': current_app.config['CLICKHOUSE_USER'],
        'password': current_app.config['CLICKHOUSE_PASSWORD'],
        'database': current_app.config['CLICKHOUSE_DATABASE'],
        'table': current_app.config['CLICKHOUSE_TABLE']
    }
    ck_client = Client(user=config_ck['user'], password=config_ck['password'], host=config_ck['host'],
                       port=config_ck['port'], database=config_ck['database'])
    res = ck_client.execute('SHOW DATABASES')
    data = {"status": 1, "info": res}
    return JsonUtil.response_success(data)


@health.route('/try_connect_hive', methods=['POST', 'GET'])
def try_connect_hive():
    con = hive.Connection(
        host=current_app.config['HIVE_HOST'],
        port=current_app.config['HIVE_PORT'],
        auth='LDAP',
        username=current_app.config['HIVE_USER'],
        password=current_app.config['HIVE_PASSWORD'],
        database=current_app.config['HIVE_DATABASE'])
    res = "1"
    print(con)
    data = {"status": 1, "info": res}
    return JsonUtil.response_success(data)


@health.route('/test_uri_count', methods=['POST', 'GET'])
def test_uri_count():
    return jsonify({"Counts": 4597871425, "code": 0})


@health.route('/test_uri_page', methods=['POST', 'GET'])
def test_uri_page():
    return jsonify({"Counts": 4597871425, "code": 0})
